<template>
  <!-- 法规标识 -->
  <div class="lawsidentification">
    <basic-container>
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="data"
        :page.sync="page"
        v-model="form"
        ref="crud"
        @search-change="searchChange"
        @search-reset="searchReset"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
      >

        <!-- 状态表格自定义 -->
        <template slot="status" slot-scope="scope">
          <el-tag size="small" type="success" v-if="scope.row['sbrq'] !== ''">已识别</el-tag>
          <el-tag size="small" type="danger" v-else>未识别</el-tag>
        </template>

        <!-- 识别表格自定义 -->
        <template slot="distinguish" slot-scope="scope">
          <el-button
            v-if="scope.row['identificationAuthority'] && scope.row['sbrq'] === ''"
            type="success"
            size="mini"
            @click="handleDistinguish(scope.row)">
            识 别
          </el-button>
          <el-button
            v-if="scope.row['identificationAuthority'] && scope.row['sbrq'] !== ''"
            type="primary"
            size="mini"
            @click="handleDoubleDistinguish(scope.row)">
            再次识别
          </el-button>
          <span v-if="!scope.row['identificationAuthority']">-</span>
        </template>

      </avue-crud>

      <!-- 识别、再次识别弹窗 -->
      <el-dialog 
        :title="dialogTitle"
        append-to-body
        :visible.sync="updateDialog"
        :close-on-click-modal="false" 
        :before-close="closeUpdateDialog"
        width="60%"
        class="law_distinguish_dialog"
      >
        <avue-form 
          ref="updateForm" 
          v-model="updateObj" 
          :option="updateOption" 
          @submit="confirmDistinguishLaw"
        >
          <template slot="menuForm">
            <el-button 
              size="small"
              icon="el-icon-close"
              @click="closeUpdateDialog()">
              取 消
            </el-button>
          </template>
        </avue-form>
      </el-dialog>

    </basic-container>
  </div>
</template>

<script>
import {
  getList,
  update
} from "@/api/securitysystemmanagement/laws";
import { mapGetters } from "vuex";
import { getDeptTree } from "@/api/system/dept";

export default {
  data() {
    return {
      form: {},
      query: {},
      data: [],
      loading: true,
      page: {
        pageSize: this.$pageSize,
        currentPage: 1,
        total: 0
      },
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 5,
        searchMenuSpan: 4,
        border: true,
        index: false,
        selection: false,
        addBtn: false,
        viewBtn: true,
        delBtn: false,
        editBtn: false,
        refreshBtn: true,
        columnBtn: false,
        dialogClickModal: false,
        menuWidth: 80,
        column: [
          {
            label: "法规名称",
            prop: "mc",
            align: "center",
            disabled: true,
            search: true,
            span: 24,
            rules: [{
              required: true,
              message: "请输入法律法规名称",
              trigger: "blur"
            }]
          },
          {
            label: "大类别",
            prop: "dlb",
            align: "center",
            disabled: true,
            width: 100,
            search: true,
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=legal_categories",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [{
              required: true,
              message: "请选择大类别",
              trigger: "change"
            }]
          },
          {
            label: "小类别",
            prop: "xlb",
            align: "center",
            disabled: true,
            search: true,
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=legal_subcategory",
            props: {
              label: "dictValue",
              value: "dictKey",
            }
          },
          {
            label: "颁布单位",
            prop: "fbjg",
            disabled: true,
            align: "center",
            hide: true
          },
          {
            label: "文件编号",
            prop: "wjbh",
            disabled: true,
            align: "center",
            hide: true
          },
          {
            label: "发布日期",
            prop: "fbrq",
            disabled: true,
            align: "center",
            width: 100,
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            hide: true
          },
          {
            label: "实施日期",
            prop: "ssrq",
            disabled: true,
            align: "center",
            width: 100,
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            hide: true
          },
          {
            label: "适用条款",
            prop: "sytk",
            align: "center",
            hide: true
          },
          {
            label: "适用部门",
            prop: "sybm",
            hide: true,
            type: "tree",
            multiple: true,
            checkStrictly: true,
            dicData: [],
            props: {
              label: "title"
            }
          },
          {
            label: "合规性评估",
            prop: "hgxpg",
            align: "center",
            span: 24
          },
          {
            label: "条款内容",
            prop: "tknr",
            align: "center",
            span: 24,
            type: "textarea",
            maxLength: 150,
            hide: true
          },
          {
            label: "Gap",
            prop: "gapcy",
            align: "center",
            span: 24,
            type: "textarea",
            maxLength: 150
          },
          {
            label: "备注",
            prop: "bz",
            align: "center",
            span: 24,
            type: "textarea",
            maxLength: 150,
            hide: true
          },
          {
            label: "识别日期",
            prop: "sbrq",
            align: "center",
            width: 100,
            display: false,
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd"
          },
          {
            label: "状态",
            prop: "status",
            align: "center",
            width: 100,
            display: false,
            slot: true
          },
          {
            label: "识别",
            prop: "distinguish",
            align: "center",
            width: 110,
            display: false,
            slot: true
          }
        ]
      },
      // 弹窗：识别、再次识别
      updateDialogType: "",
      distinguishStr: "distinguish",
      doubledistinguishStr: "doubledistinguish",
      dialogTitle: "",
      updateDialog: false,
      updateObj: {},
    };
  },
  computed: {
    ...mapGetters(["permission"]),

    updateOption() {
      return {
        size: "small",
        submitText: "识 别",
        mockBtn: false,
        printBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "法规名称",
            prop: "mc",
            disabled: true,
            span: 24,
            rules: [{
              required: true,
              message: "请输入法律法规名称",
              trigger: "blur"
            }]
          },
          {
            label: "大类别",
            prop: "dlb",
            disabled: true,
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=legal_categories",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [{
              required: true,
              message: "请选择大类别",
              trigger: "change"
            }]
          },
          {
            label: "小类别",
            prop: "xlb",
            disabled: true,
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=legal_subcategory",
            props: {
              label: "dictValue",
              value: "dictKey",
            }
          },
          {
            label: "颁布单位",
            prop: "fbjg",
            disabled: true
          },
          {
            label: "文件编号",
            prop: "wjbh",
            disabled: true
          },
          {
            label: "发布日期",
            prop: "fbrq",
            disabled: true,
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd"
          },
          {
            label: "实施日期",
            prop: "ssrq",
            disabled: true,
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd"
          },
          {
            label: "适用条款",
            prop: "sytk"
          },
          {
            label: "适用部门",
            prop: "sybm",
            type: "tree",
            multiple: true,
            checkStrictly: true,
            dicData: [],
            props: {
              label: "title"
            }
          },
          {
            label: "合规性评估",
            prop: "hgxpg",
            span: 24
          },
          {
            label: "条款内容",
            prop: "tknr",
            span: 24,
            type: "textarea",
            maxLength: 150
          },
          {
            label: "Gap",
            prop: "gapcy",
            span: 24,
            type: "textarea",
            maxLength: 150
          },
          {
            label: "备注",
            prop: "bz",
            span: 24,
            type: "textarea",
            maxLength: 150
          }
        ]
      }
    }
  },
  mounted() {
    this.initData();
  },
  methods: {

    // 初始化页面
    initData() {
      var tenantId = JSON.parse(localStorage.getItem("saber-tenantId")).content;
      getDeptTree(tenantId).then(res => {
        const sybmColumn = this.findObject(this.option.column, "sybm");
        const sybmDialog = this.findObject(this.updateOption.column, "sybm");
        sybmColumn.dicData = res.data.data;
        sybmDialog.dicData = res.data.data;
      }).then(() => {
        this.onLoad(this.page);
      });
    },

    // 清空搜索条件
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },

    // 搜索条件改变
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },

    // 更换页数
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },

    // 更换每页显示数量
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },

    // 刷新
    refreshChange() {
      this.onLoad(this.page, this.query);
    },

    // 加载数据
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },

    /**
     * 弹窗：识别、再次识别
    */

    // 打开识别弹窗
    handleDistinguish(row) {
      this.updateDialogType = this.distinguishStr;
      this.updateDialog = true;
      this.dialogTitle = "识别法规";
      this.updateObj = this.formatUpdateForm(row);
    },

    // 打开再次识别弹窗
    handleDoubleDistinguish(row) {
      this.updateDialogType = this.doubledistinguishStr;
      this.updateDialog = true;
      this.dialogTitle = "再次识别法规";
      this.updateObj = this.formatUpdateForm(row);
    },

    // 格式化识别、再次识别弹窗表单数据
    formatUpdateForm(row) {
      var data = {};
      for(var key in row) {
        data[key] = row[key];
      }
      return data;
    },

    // 关闭、取消识别&再次识别弹窗
    closeUpdateDialog() {
      this.updateDialogType = "";
      this.updateDialog = false;
      this.dialogTitle = "";
      this.updateObj = {};
    },

    // 确认识别法规
    confirmDistinguishLaw() {
      this.$refs.updateForm.validate((vaild,done)=>{
        if(vaild){
          var updateData = this.formatUpdateData(this.updateObj);
          update(updateData).then(res => {
            if(res.data.success) {
              this.closeUpdateDialog();
              this.page.currentPage = 1;
              this.onLoad(this.page);
            }
          });
          done();
        }
      })
    },

    // 获取识别数据
    formatUpdateData(row) {
      var data = {};
      for(var key in row) {
        data[key] = row[key];
      }
      return data;
    },

  }
};
</script>

<style lang="scss" scpoed>
.law_distinguish_dialog {
  .avue-form__menu--center {
    text-align: center;
  }
}
</style>